/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of JMail                                              *
 * Copyright (C) 2002-2003 Yvan Norsa <norsay@wanadoo.fr>                  *
 *                                                                         *
 * JMail is free software; you can redistribute it and/or modify           *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 2 of the License, or       *
 * any later version.                                                      *
 *                                                                         *
 * JMail is distributed in the hope that it will be useful,                *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License along *
 * with JMail; if not, write to the Free Software Foundation, Inc.,        *
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                 *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

/** Dialog used to edit a profile */
final class EditProfile extends JDialog {
	/** Language resource */
	private ResourceBundle msgBundle;

	private JPanel panel;

	private JTabbedPane tabbedPane;

	private JPanel miscTab;
	private JPanel serverTab;
	private JPanel ldapTab;

	private JPanel buttons;

	private JButton ok;
	private JButton cancel;

	private Profile profile;

	private EditProfileListener listener;

	private JLabel profilePasswordLabel;
	private JPasswordField profilePasswordField;

	private JLabel loginLabel;
	private JTextField loginField;
	private JLabel passwordLabel;
	private JPasswordField passwordField;
	private JLabel emailLabel;
	private JTextField emailField;
	private JCheckBox signUsing;
	private JTextField signFileField;
	private JButton signFileButton;

	private JLabel replyToLabel;
	private JTextField replyToField;

	private JLabel typesLabel;
	private String[] types;
	private JComboBox typesBox;
	private JLabel inLabel;
	private JLabel inPortLabel;
	private JLabel useSSLLabel;
	private JTextField inField;
	private JTextField inPortField;
	private JCheckBox useSSLBox;
	private JLabel outLabel;
	private JLabel outPortLabel;
	private JTextField outField;
	private JTextField outPortField;

	private JCheckBox ldapUsing;
	private JLabel urlLabel;
	private JLabel portLabel;
	private JTextField urlField;
	private JTextField portField;
	private JLabel suffixLabel;
	private JTextField suffixField;
	private JLabel addSuffixLabel;
	private JCheckBox addSuffixToURLBox;
	private JCheckBox addSuffixToBaseBox;
	private JCheckBox addSuffixToLoginBox;
	private JLabel baseLabel;
	private JTextField baseField;
	private JLabel ldapLoginLabel;
	private JTextField ldapLoginField;
	private JLabel ldapPasswordLabel;
	private JPasswordField ldapPasswordField;
	private JLabel nameLabel;
	private JTextField nameField;
	private JLabel mailLabel;
	private JTextField mailField;
	private JLabel supLabel;
	private JTextField supField;
	private JButton add;
	private JList supList;
	private JScrollPane scrollPane;
	private Vector fields;

	private boolean hasChanged;

	/**
	 * Constructor
	 * 
	 * @param profile
	 *            user profile
	 * @param msgBundle
	 *            language resource
	 */
	protected EditProfile(Profile profile, ResourceBundle msgBundle) {
		super((JFrame) null, msgBundle.getString("EditProfile.frameTitle"),
				true);

		this.msgBundle = msgBundle;

		hasChanged = true;

		this.profile = profile;

		listener = new EditProfileListener();

		panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

		tabbedPane = new JTabbedPane();

		miscTab = makeMiscTab();
		tabbedPane.addTab("Misc", miscTab);

		serverTab = makeServerTab();
		tabbedPane.addTab("Server", serverTab);

		ldapTab = makeLdapTab();
		tabbedPane.addTab("LDAP", ldapTab);

		panel.add(tabbedPane);

		buttons = new JPanel();

		ok = new JButton("OK");
		ok.addActionListener(listener);
		buttons.add(ok);

		cancel = new JButton(msgBundle.getString("EditProfile.cancelLabel"));
		cancel.addActionListener(listener);
		buttons.add(cancel);

		panel.add(buttons);

		setContentPane(panel);

		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		pack();
		show();
	}

	/**
	 * Build the misc tab
	 * 
	 * @return misc JPanel
	 */
	private JPanel makeMiscTab() {
		JPanel panel = new JPanel();

		JPanel panel2 = new JPanel();

		GridBagLayout gridBag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();

		panel2.setLayout(gridBag);

		Common.buildConstraints(constraints, 1, 0, 1, 1, 0, 0);
		profilePasswordLabel = new JLabel(
				msgBundle.getString("EditProfile.profilePasswordText"));
		gridBag.setConstraints(profilePasswordLabel, constraints);
		panel2.add(profilePasswordLabel);

		Common.buildConstraints(constraints, 1, 1, 1, 1, 0, 0);
		profilePasswordField = new JPasswordField(
				profile.getUnhashedProfilePassword(), 20);
		gridBag.setConstraints(profilePasswordField, constraints);
		panel2.add(profilePasswordField);

		Common.buildConstraints(constraints, 1, 2, 1, 1, 0, 0);
		loginLabel = new JLabel("Login :");
		gridBag.setConstraints(loginLabel, constraints);
		panel2.add(loginLabel);

		Common.buildConstraints(constraints, 1, 3, 1, 1, 0, 0);
		loginField = new JTextField(profile.getLogin(), 20);
		gridBag.setConstraints(loginField, constraints);
		panel2.add(loginField);

		Common.buildConstraints(constraints, 1, 4, 1, 1, 0, 0);
		passwordLabel = new JLabel(
				msgBundle.getString("EditProfile.mailPasswordText"));
		gridBag.setConstraints(passwordLabel, constraints);
		panel2.add(passwordLabel);

		Common.buildConstraints(constraints, 1, 5, 1, 1, 0, 0);
		passwordField = new JPasswordField(profile.getDecryptedMailPassword(),
				20);
		gridBag.setConstraints(passwordField, constraints);
		panel2.add(passwordField);

		Common.buildConstraints(constraints, 1, 6, 1, 1, 0, 0);
		emailLabel = new JLabel("Email :");
		gridBag.setConstraints(emailLabel, constraints);
		panel2.add(emailLabel);

		Common.buildConstraints(constraints, 1, 7, 1, 1, 0, 0);
		emailField = new JTextField(profile.getEmail(), 20);
		gridBag.setConstraints(emailField, constraints);
		panel2.add(emailField);

		boolean signUsed = (profile.getSignature() != null && profile
				.getSignature().compareTo("") != 0);

		Common.buildConstraints(constraints, 0, 8, 1, 1, 0, 0);
		signUsing = new JCheckBox(
				msgBundle.getString("EditProfile.signUsingText"), signUsed);
		gridBag.setConstraints(signUsing, constraints);
		signUsing.addItemListener(listener);
		panel2.add(signUsing);

		Common.buildConstraints(constraints, 1, 8, 1, 1, 0, 0);
		signFileField = new JTextField(profile.getSignature(), 20);
		gridBag.setConstraints(signFileField, constraints);
		signFileField.setEnabled(signUsed);
		panel2.add(signFileField);

		Common.buildConstraints(constraints, 2, 8, 1, 1, 0, 0);
		signFileButton = new JButton(
				msgBundle.getString("EditProfile.signFileButtonText"));
		gridBag.setConstraints(signFileButton, constraints);
		signFileButton.setEnabled(signUsed);
		signFileButton.addActionListener(listener);
		panel2.add(signFileButton);

		Common.buildConstraints(constraints, 1, 9, 1, 1, 0, 0);
		replyToLabel = new JLabel("Reply-To :");
		gridBag.setConstraints(replyToLabel, constraints);
		panel2.add(replyToLabel);

		Common.buildConstraints(constraints, 1, 10, 1, 1, 0, 0);
		replyToField = new JTextField(profile.getReplyTo(), 20);
		gridBag.setConstraints(replyToField, constraints);
		panel2.add(replyToField);

		panel.add(panel2);

		return (panel);
	}

	/**
	 * Build the server tab
	 * 
	 * @return server JPanel
	 */
	private JPanel makeServerTab() {
		JPanel panel = new JPanel();

		JPanel panel2 = new JPanel();

		GridBagLayout gridBag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();

		panel2.setLayout(gridBag);

		Common.buildConstraints(constraints, 0, 0, 1, 1, 80, 100);
		typesLabel = new JLabel(
				msgBundle.getString("EditProfile.typesLabelText"));
		gridBag.setConstraints(typesLabel, constraints);
		panel2.add(typesLabel);

		Common.buildConstraints(constraints, 1, 0, 1, 1, 20, 0);
		types = new String[2];
		types[0] = "pop3";
		types[1] = "imap";

		typesBox = new JComboBox(types);
		typesBox.setEditable(false);
		typesBox.setSelectedItem(profile.getType());
		typesBox.addActionListener(listener);
		gridBag.setConstraints(typesBox, constraints);
		panel2.add(typesBox);

		Common.buildConstraints(constraints, 0, 1, 1, 1, 0, 0);
		inLabel = new JLabel(msgBundle.getString("EditProfile.inLabelText"));
		gridBag.setConstraints(inLabel, constraints);
		panel2.add(inLabel);

		Common.buildConstraints(constraints, 1, 1, 1, 1, 0, 0);
		inPortLabel = new JLabel("Port :");
		gridBag.setConstraints(inPortLabel, constraints);
		panel2.add(inPortLabel);

		Common.buildConstraints(constraints, 2, 1, 1, 1, 0, 0);
		useSSLLabel = new JLabel(msgBundle.getString("EditProfile.useSSLText"));
		gridBag.setConstraints(useSSLLabel, constraints);
		panel2.add(useSSLLabel);

		Common.buildConstraints(constraints, 0, 2, 1, 1, 0, 0);
		inField = new JTextField(profile.getIncoming(), 20);
		gridBag.setConstraints(inField, constraints);
		panel2.add(inField);

		Common.buildConstraints(constraints, 1, 2, 1, 1, 0, 0);
		inPortField = new JTextField(String.valueOf(profile.getIncomingPort()),
				4);
		gridBag.setConstraints(inPortField, constraints);
		panel2.add(inPortField);

		Common.buildConstraints(constraints, 2, 2, 1, 1, 0, 0);
		useSSLBox = new JCheckBox();
		useSSLBox.setSelected(profile.getUseSSL());
		useSSLBox.addItemListener(listener);
		gridBag.setConstraints(useSSLBox, constraints);
		panel2.add(useSSLBox);

		Common.buildConstraints(constraints, 0, 3, 1, 1, 0, 0);
		outLabel = new JLabel(msgBundle.getString("EditProfile.outLabelText"));
		gridBag.setConstraints(outLabel, constraints);
		panel2.add(outLabel);

		Common.buildConstraints(constraints, 1, 3, 1, 1, 0, 0);
		outPortLabel = new JLabel("Port :");
		gridBag.setConstraints(outPortLabel, constraints);
		panel2.add(outPortLabel);

		Common.buildConstraints(constraints, 0, 4, 1, 1, 0, 0);
		outField = new JTextField(profile.getOutgoing(), 20);
		gridBag.setConstraints(outField, constraints);
		panel2.add(outField);

		Common.buildConstraints(constraints, 1, 4, 1, 1, 0, 0);
		outPortField = new JTextField(
				String.valueOf(profile.getOutgoingPort()), 4);
		gridBag.setConstraints(outPortField, constraints);
		panel2.add(outPortField);

		panel.add(panel2);

		return (panel);
	}

	/**
	 * Build the ldap tab
	 * 
	 * @return ldap JPanel
	 */
	private JPanel makeLdapTab() {
		fields = new Vector();

		JPanel panel = new JPanel();

		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

		boolean ldapUser = profile.getLdapUser();

		ldapUsing = new JCheckBox(
				msgBundle.getString("EditProfile.ldapUsingLabel"), ldapUser);
		ldapUsing.addItemListener(listener);
		panel.add(ldapUsing);

		JPanel content = new JPanel();

		GridBagLayout gridBag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();

		content.setLayout(gridBag);

		Common.buildConstraints(constraints, 0, 0, 1, 1, 80, 100);
		urlLabel = new JLabel(msgBundle.getString("EditProfile.urlLabelText"));
		gridBag.setConstraints(urlLabel, constraints);
		content.add(urlLabel);

		Common.buildConstraints(constraints, 1, 0, 1, 1, 20, 0);
		portLabel = new JLabel("Port :");
		gridBag.setConstraints(portLabel, constraints);
		content.add(portLabel);

		Common.buildConstraints(constraints, 0, 1, 1, 1, 0, 0);
		urlField = new JTextField(20);
		gridBag.setConstraints(urlField, constraints);
		content.add(urlField);

		Common.buildConstraints(constraints, 1, 1, 1, 1, 0, 0);
		portField = new JTextField(4);
		gridBag.setConstraints(portField, constraints);
		content.add(portField);

		Common.buildConstraints(constraints, 0, 2, 1, 1, 0, 0);
		suffixLabel = new JLabel(
				msgBundle.getString("EditProfile.suffixLabelText"));
		gridBag.setConstraints(suffixLabel, constraints);
		content.add(suffixLabel);

		Common.buildConstraints(constraints, 0, 3, 1, 1, 0, 0);
		suffixField = new JTextField(20);
		gridBag.setConstraints(suffixField, constraints);
		content.add(suffixField);

		Common.buildConstraints(constraints, 0, 4, 1, 1, 0, 0);
		addSuffixLabel = new JLabel(
				msgBundle.getString("EditProfile.addSuffixLabelText"));
		gridBag.setConstraints(addSuffixLabel, constraints);
		content.add(addSuffixLabel);

		Common.buildConstraints(constraints, 0, 5, 1, 1, 0, 0);
		addSuffixToURLBox = new JCheckBox("URL");
		gridBag.setConstraints(addSuffixToURLBox, constraints);
		content.add(addSuffixToURLBox);

		Common.buildConstraints(constraints, 1, 5, 1, 1, 0, 0);
		addSuffixToBaseBox = new JCheckBox("Base");
		gridBag.setConstraints(addSuffixToBaseBox, constraints);
		content.add(addSuffixToBaseBox);

		Common.buildConstraints(constraints, 2, 5, 1, 1, 0, 0);
		addSuffixToLoginBox = new JCheckBox("Login");
		gridBag.setConstraints(addSuffixToLoginBox, constraints);
		content.add(addSuffixToLoginBox);

		Common.buildConstraints(constraints, 0, 6, 1, 1, 0, 0);
		baseLabel = new JLabel(msgBundle.getString("EditProfile.baseLabelText"));
		gridBag.setConstraints(baseLabel, constraints);
		content.add(baseLabel);

		Common.buildConstraints(constraints, 0, 7, 1, 1, 0, 0);
		baseField = new JTextField(20);
		gridBag.setConstraints(baseField, constraints);
		content.add(baseField);

		Common.buildConstraints(constraints, 0, 8, 1, 1, 0, 0);
		ldapLoginLabel = new JLabel("Login :");
		gridBag.setConstraints(ldapLoginLabel, constraints);
		content.add(ldapLoginLabel);

		Common.buildConstraints(constraints, 0, 9, 1, 1, 0, 0);
		ldapLoginField = new JTextField(20);
		gridBag.setConstraints(ldapLoginField, constraints);
		content.add(ldapLoginField);

		Common.buildConstraints(constraints, 0, 10, 1, 1, 0, 0);
		ldapPasswordLabel = new JLabel(
				msgBundle.getString("EditProfile.ldapPasswordText"));
		gridBag.setConstraints(ldapPasswordLabel, constraints);
		content.add(ldapPasswordLabel);

		Common.buildConstraints(constraints, 0, 11, 1, 1, 0, 0);
		ldapPasswordField = new JPasswordField(20);
		gridBag.setConstraints(ldapPasswordField, constraints);
		content.add(ldapPasswordField);

		Common.buildConstraints(constraints, 0, 12, 1, 1, 0, 0);
		nameLabel = new JLabel(msgBundle.getString("EditProfile.nameLabelText"));
		gridBag.setConstraints(nameLabel, constraints);
		content.add(nameLabel);

		Common.buildConstraints(constraints, 0, 13, 1, 1, 0, 0);
		nameField = new JTextField(20);
		gridBag.setConstraints(nameField, constraints);
		content.add(nameField);

		Common.buildConstraints(constraints, 0, 14, 1, 1, 0, 0);
		mailLabel = new JLabel(msgBundle.getString("EditProfile.mailLabelText"));
		gridBag.setConstraints(mailLabel, constraints);
		content.add(mailLabel);

		Common.buildConstraints(constraints, 0, 15, 1, 1, 0, 0);
		mailField = new JTextField(20);
		gridBag.setConstraints(mailField, constraints);
		content.add(mailField);

		Common.buildConstraints(constraints, 0, 16, 1, 1, 0, 0);
		supLabel = new JLabel(msgBundle.getString("EditProfile.supLabelText"));
		gridBag.setConstraints(supLabel, constraints);
		content.add(supLabel);

		Common.buildConstraints(constraints, 0, 17, 1, 1, 0, 0);
		supField = new JTextField(20);
		gridBag.setConstraints(supField, constraints);
		content.add(supField);

		Common.buildConstraints(constraints, 1, 17, 1, 1, 0, 0);
		add = new JButton(msgBundle.getString("EditProfile.addLabel"));
		add.addActionListener(listener);
		gridBag.setConstraints(add, constraints);
		content.add(add);

		Common.buildConstraints(constraints, 2, 17, 1, 1, 0, 0);
		supList = new JList();

		if (ldapUser) {
			urlField.setText(profile.getUrl());
			portField.setText(String.valueOf(profile.getPort()));
			suffixField.setText(profile.getSuffix());
			addSuffixToURLBox.setSelected(profile.getAddSuffixToURL());
			addSuffixToBaseBox.setSelected(profile.getAddSuffixToBase());
			addSuffixToLoginBox.setSelected(profile.getAddSuffixToLogin());
			baseField.setText(profile.getBase());
			ldapLoginField.setText(profile.getLdapLogin());
			ldapPasswordField.setText(profile.getLdapPassword());
			nameField.setText(profile.getLdapNameField());
			mailField.setText(profile.getLdapMailField());

			if (profile.getNbSupFields() > 0) {
				String[] supFields = profile.getSupFields();

				for (int i = 0; i < supFields.length; i++)
					fields.add(supFields[i]);

				supList.setListData(fields);
			}
		}

		scrollPane = new JScrollPane(supList);
		gridBag.setConstraints(scrollPane, constraints);
		content.add(scrollPane);

		setLdapContentEnabled(ldapUser);

		panel.add(content);

		return (panel);
	}

	/**
	 * Allows to disable or enable the content of the ldap tab
	 * 
	 * @param boolean enable or disable
	 */
	private void setLdapContentEnabled(boolean enabled) {
		urlLabel.setEnabled(enabled);
		portLabel.setEnabled(enabled);
		urlField.setEnabled(enabled);
		portField.setEnabled(enabled);
		suffixLabel.setEnabled(enabled);
		suffixField.setEnabled(enabled);
		addSuffixLabel.setEnabled(enabled);
		addSuffixToURLBox.setEnabled(enabled);
		addSuffixToBaseBox.setEnabled(enabled);
		addSuffixToLoginBox.setEnabled(enabled);
		baseLabel.setEnabled(enabled);
		baseField.setEnabled(enabled);
		ldapLoginLabel.setEnabled(enabled);
		ldapLoginField.setEnabled(enabled);
		ldapPasswordLabel.setEnabled(enabled);
		ldapPasswordField.setEnabled(enabled);
		nameLabel.setEnabled(enabled);
		nameField.setEnabled(enabled);
		mailLabel.setEnabled(enabled);
		mailField.setEnabled(enabled);
		supLabel.setEnabled(enabled);
		supField.setEnabled(enabled);
		add.setEnabled(enabled);
		supList.setEnabled(enabled);
		scrollPane.setEnabled(enabled);
	}

	private void setSignContentEnabled(boolean enabled) {
		signFileField.setEnabled(enabled);
		signFileButton.setEnabled(enabled);
	}

	/**
	 * Tells wether changes were made or not
	 * 
	 * @return boolean : if true, write modifications
	 */
	protected final boolean hasChanged() {
		return (hasChanged);
	}

	/**
	 * Return the new profile if needed
	 * 
	 * @return profile with modifications
	 */
	protected final Profile getModifiedProfile() {
		return (profile);
	}

	private final void setIncomingPort(String protocol, boolean withSSL) {
		int portNumber = -1;

		if (protocol.compareTo("pop3") == 0) {
			if (withSSL) {
				portNumber = 995;
			} else {
				portNumber = 110;
			}
		}

		else

		{
			if (withSSL) {
				portNumber = 993;
			} else {
				portNumber = 143;
			}
		}

		inPortField.setText(String.valueOf(portNumber));
	}

	/**
	 * Adds an entry in the LDAP directory
	 * 
	 * @param profile
	 *            user profile
	 * @param name
	 *            name to add
	 * @param mail
	 *            mail address to add
	 * @param fields
	 *            extra fields
	 */
	protected final static void addEntry(Profile profile, String name,
			String mail, String fields[]) {
		StringBuffer url = new StringBuffer(profile.getUrl() + ":"
				+ profile.getPort());
	
		String suffix = profile.getSuffix();
	
		if (profile.getAddSuffixToURL())
			url.append("/" + suffix);
	
		StringBuffer base = new StringBuffer(profile.getBase());
	
		if (profile.getAddSuffixToBase())
			base.append(", " + suffix);
	
		StringBuffer login = new StringBuffer(profile.getLdapLogin());
	
		if (profile.getAddSuffixToLogin())
			login.append(", " + suffix);
	
		String fields2[] = new String[fields.length * 2];
	
		int nb = profile.getNbSupFields();
		String[] origSupFields = profile.getSupFields();
	
		for (int i = 0; i < nb; i++) {
			fields2[(2 * i)] = origSupFields[i];
			fields2[(2 * i) + 1] = fields[i];
		}
	
		LdapSearch.add(url.toString(), base.toString(), profile.getLdapNameField(),
				profile.getLdapMailField(), login.toString(),
				profile.getLdapPassword(), name, mail, fields2);
	}

	/** Listener for this class */
	private final class EditProfileListener extends KeyAdapter implements
			ActionListener, ItemListener {
		/**
		 * Method invoked when the user types a key
		 * 
		 * @param e
		 *            event triggered
		 */
		public final void keyReleased(KeyEvent e) {
			// System.out.println("keyReleased..");
			hasChanged = true;
		}

		/**
		 * This method is invoked when an event is triggered
		 * 
		 * @param e
		 *            event
		 */
		public final void actionPerformed(ActionEvent e) {
			JComponent c = (JComponent) e.getSource();

			if (c == signFileButton) {
				JFileChooser fc = new JFileChooser();

				int val = fc.showOpenDialog(null);

				if (val == JFileChooser.APPROVE_OPTION) {
					File file = fc.getSelectedFile();

					if (file != null) {
						String name = file.getPath();
						signFileField.setText(name);
					}
				}
			}

			else if (c == typesBox) {
				setIncomingPort((String) typesBox.getSelectedItem(),
						useSSLBox.isSelected());
			} else if (c == add) {
				String newField = supField.getText();

				if (newField.compareTo("") != 0) {
					supField.setText("");
					fields.add(newField);
					supList.setListData(fields);
				}
			}

			else if (c == ok) {
				if (hasChanged) {
					char profilePass[] = profilePasswordField.getPassword();
					String profilePassword = String.valueOf(profilePass).trim();

					char pass[] = passwordField.getPassword();
					boolean ldapUser = ldapUsing.isSelected();

					String sign = "";

					if (signUsing.isSelected()) {
						sign = signFileField.getText();
					}
					profile.setBaseInfos(JMailSecurity
							.hashAndEncode(profilePassword), emailField
							.getText().trim(), replyToField.getText().trim(),
							(String) typesBox.getSelectedItem(), inField
									.getText().trim(), Integer
									.parseInt(inPortField.getText().trim()),
							useSSLBox.isSelected(), outField.getText().trim(),
							Integer.parseInt(outPortField.getText().trim()),
							loginField.getText().trim(), JMailSecurity
									.cryptAndEncode(profilePassword, String
											.valueOf(pass).trim()), sign,
							ldapUser);

					if (ldapUser) {
						char ldapPass[] = ldapPasswordField.getPassword();

						ListModel model = supList.getModel();
						int nb = model.getSize();
						LdapInfo ldapInfo = new LdapInfo();
						ldapInfo.setUrl(urlField.getText().trim());
						ldapInfo.setPort(Integer.parseInt(portField.getText()
								.trim()));
						ldapInfo.setSuffix(suffixField.getText().trim());
						ldapInfo.setAddSuffixToURL(addSuffixToURLBox
								.isSelected());
						ldapInfo.setAddSuffixToBase(addSuffixToURLBox
								.isSelected());
						ldapInfo.setAddSuffixToLogin(addSuffixToLoginBox
								.isSelected());
						ldapInfo.setBase(baseField.getText().trim());
						ldapInfo.setLdapLogin(ldapLoginField.getText().trim());
						ldapInfo.setLdapNameField(nameField.getText().trim());
						ldapInfo.setLdapMailField(mailField.getText().trim());
						ldapInfo.setLdapPassword(ldapPasswordField.getText()
								.trim());
						ldapInfo.setNbSupFields(nb);

						profile.setLdapInfos(ldapInfo);

						if (nb > 0) {
							Vector newFields = new Vector(nb);

							String field = null;

							for (int i = 0; i < nb; i++) {
								field = (String) model.getElementAt(i);
								newFields.add(field);
							}

							profile.setSupFieldsVector(newFields);
						}
					}
				}

				hide();
			}

			else if (c == cancel) {
				hasChanged = false;
				hide();
			}
		}

		/**
		 * Method called when the user checks a box
		 * 
		 * @param e
		 *            event triggered
		 */
		public final void itemStateChanged(ItemEvent e) {
			JCheckBox c = (JCheckBox) e.getSource();

			if (c == signUsing) {
				setSignContentEnabled(signUsing.isSelected());
			} else if (c == ldapUsing) {
				setLdapContentEnabled(ldapUsing.isSelected());
			} else if (c == useSSLBox) {
				setIncomingPort((String) typesBox.getSelectedItem(),
						useSSLBox.isSelected());
			}
		}
	}
}
